package com.anjoyo.xgh.musicshare.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;

import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.framework.AbstractWeibo.ShareParams;
import cn.sharesdk.sina.weibo.SinaWeibo;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.adapter.MyPagerAdapter;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.MusicSpf;

public class WelcomeAct extends Activity {
	Context con;
	ViewPager pager;
	LayoutInflater inflater;
	MyPagerAdapter adapter;
	ImageView imageView,ivwindmill,ivpag1text,ivfeiji,ivpag2text1,ivpag2text2,
				ivpag3text,ivweibotext,ivgo;
	CheckBox cbistoweibo;
	Animation animarotate,animain,animafeiji;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcomepager_layout);
		con = this;
		AbstractWeibo.initSDK(this);
		getImg();
		imageView = (ImageView) findViewById(R.id.iv_whichpag);
		pager = (ViewPager) findViewById(R.id.pager_welcome);
		List<View> list = new ArrayList<View>();
		inflater = LayoutInflater.from(this);
		View v1 = inflater.inflate(R.layout.welcome1_layout, null);
		View v2 = inflater.inflate(R.layout.welcome2_layout, null);
		View v3 = inflater.inflate(R.layout.welcome3_layout, null);
		list.add(v1);
		list.add(v2);
		list.add(v3);
		adapter = new MyPagerAdapter(list);
		pager.setAdapter(adapter);
		//v1中的控件
		ivwindmill = (ImageView) v1.findViewById(R.id.iv_windmill);
		ivpag1text = (ImageView) v1.findViewById(R.id.iv_pag1text);
		//v2中的控件
		ivfeiji = (ImageView) v2.findViewById(R.id.iv_feiji);
		ivpag2text1= (ImageView) v2.findViewById(R.id.iv_pag2text1);
		ivpag2text2 = (ImageView) v2.findViewById(R.id.iv_pag2text2);
		//v3中的控件
		ivpag3text = (ImageView) v3.findViewById(R.id.iv_pag3text);
		cbistoweibo = (CheckBox) v3.findViewById(R.id.cb_istoweibo);
		ivweibotext = (ImageView) v3.findViewById(R.id.iv_weibotext);
		ivgo = (ImageView) v3.findViewById(R.id.iv_go);
		//动画
		animarotate = AnimationUtils.loadAnimation(this,R.anim.rotate);
		animain = AnimationUtils.loadAnimation(this,R.anim.fade_in);
		animafeiji = AnimationUtils.loadAnimation(this,R.anim.flyaway);
		//设置首页动画
		ivwindmill.startAnimation(animarotate);
		ivpag1text.startAnimation(animain);
		
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					imageView.setImageResource(R.drawable.sc_zs1);
					ivwindmill.startAnimation(animarotate);
					ivpag1text.startAnimation(animain);
					break;
				case 1:
					imageView.setImageResource(R.drawable.sc_zs2);
					ivpag2text1.startAnimation(animain);
					ivpag2text2.startAnimation(animain);
					ivfeiji.startAnimation(animafeiji);
					break;
				case 2:
					imageView.setImageResource(R.drawable.sc_zs3);
					ivpag3text.startAnimation(animain);
					break;
				default:
					break;
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
//				System.out.println(arg0+"--"+arg1+"--"+arg2);
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		ivgo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (cbistoweibo.isChecked()) {
					ShareParams params = new ShareParams();
					params.imagePath=Constant.CACHE+"share.jpg";
					params.text = "自己做的安卓音乐分享";
					SinaWeibo sinaWeibo = new SinaWeibo(con);
					sinaWeibo.share(params);
//					AbstractWeibo weibo=AbstractWeibo.getWeibo(con, SinaWeibo.NAME);
//					weibo.share(params);
				}
				startActivity(new Intent(con,LoginAct.class));
				MusicSpf.setisshowedWelcome(con, true);
				finish();
			}
		});
	}
	
	public void getImg() {
		InputStream is;
		try {
			is = getAssets().open("sc_xg.jpg");
			Bitmap bitmap = BitmapFactory.decodeStream(is);
			File outFile = new File(Constant.CACHE+"share.jpg");
			if (!outFile.exists()) {
				outFile.getParentFile().mkdirs();
				outFile.createNewFile();
			}
			FileOutputStream os = new FileOutputStream(outFile);
			if (bitmap!=null) {
				bitmap.compress(CompressFormat.JPEG, 100, os);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
